<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $framework;
require_once("webapp/application/application.php");
require_once("webapp/helpers/cart.helpers.php");
require_once("$framework/paginator.php");
require_once("$framework/session.php");
/**
 * catalog manager for this little demonstration site.
 * the <em>catalogManager.php</em> is the catalog handler for products displayed on the
 * <strong>show.tpl</strong>, corresponding to the "<strong>home</strong>" page.
 * @author frederic
 *
 */
class CatalogManager extends Application{
	
	protected $products;
	protected $categories;
	
	public function initialize(){
		// load categories list
		$this->products=__query_entities(
				"product",
					$where = "",
					$order = $order = array(	array('field'=>"availableat_date",'comparator'=>"ASC")),
					$limit = array("max"=>6)
				);
		$this->addEntity('product',$this->products);
		__template_set('products',self::$_entities['product']);
		// Cart management
		$this->cart = CartHelpers::get();
		__template_set("cart",$this->cart);
	}
	
	public function show($type,$id){

		$this->product = __entity_associates($type,$id,array('resource'));
		//echo "<pre>".print_r($product,true)."</pre>";
		__template_set("product",$this->product);
		
		$cart = CartHelpers::get();
		__template_set("cart",$cart);

		$this->categories = __query_entities(	"category",
			$where = array(array('field'=>"category_path",'comparator'=>"=",'value'=>$this->product->category_tree)),
			$order = array(	array('field'=>"parent_id",'comparator'=>"ASC"),
				 			array('field'=>"sortorder",'comparator'=>"ASC")),
			$limit = "");
		__template_set("categories",$this->categories);
		
		
		return "catalog/show";
	}
	
	public function lister($type,$id){
		$this->categories = __query_entities(
				"category",
					$where = array(array('field'=>"category_path",'comparator'=>"like",'value'=>"$id.%")),
					$order = array(	array('field'=>"parent_id",'comparator'=>"ASC"),
						 			array('field'=>"sortorder",'comparator'=>"ASC")),
					$limit = "");
		$this->setEntity('category',$this->categories);
		
		$this->products = __query_entities(
				"product",
					$where = array( array('field'=>"category_tree",'comparator'=>"like",'value'=>"%".$id.".%"),
									//array('field'=>"availableat_date",'comparator'=>"<",'value'=>date("yyyy-mm-dd hh:ii"))
									),
					$order = $order = array(	array('field'=>"availableat_date",'comparator'=>"ASC")),
					$limit = array("max"=>6));
		$this->setEntity('product',$this->products);

		__template_set('categories',self::$_entities['category']);
		__template_set('products',self::$_entities['product']);

		// Cart management
		$this->cart = CartHelpers::get();
		__template_set("cart",$this->cart);
		
		return "catalog/list";
	}
}
?>